import axios from "axios";
import { Message } from "element-ui";
import router from "../router";

// axios.defaults.baseURL = 'http://backend-api-01.newbee.ltd/api/v1'
axios.defaults.baseURL = "http://localhost:8080/api/v1";

axios.defaults.withCredentials = true;
axios.defaults.headers["X-Requested-With"] = "XMLHttpRequest";
// axios.defaults.headers['token'] = localStorage.getItem('token') || ''
axios.defaults.headers.post["Content-Type"] = "application/json";

axios.interceptors.response.use((res) => {
  if (res.data.resultCode != 200) {
    if (res.data.message) Message.error(res.data.message);
    if (res.data.resultCode == 416) {
      router.push({ path: "/login" });
    }
    return Promise.reject(res.data);
  }

  return res.data;
});

export default axios;
